// pages/mine/setting/CP/CP.js
const app = getApp();
const db = wx.cloud.database()
const _ = db.command
const userinfoDB=db.collection('userlogin')
let tem_head_img =[]  //图片数组
const ctx = wx.createCanvasContext('myCanvas')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [], // 上传的图片
    head_img: [],
    userPhone:null,
  },

  uploadImg: function () {
    let that = this
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        const _imgpath = res.tempFilePaths
        console.log(res)
          let _image= []
          that.setData({
            images: _image.concat(_imgpath)
          });
      }
    })
  },

  submit: function () {
    let that = this
    console.log(that.data.images)
    if(tem_head_img[0]==that.data.images[0]){
      wx.showToast({
        title: '你还未选择照片',
        icon:"none"
      })
    }else{
      wx.showLoading({
        title: '上传中',
      })
    // 上传图片到云存储
    let promiseArr = [];
    for (let i = 0; i < that.data.images.length; i++) {
      promiseArr.push(new Promise((reslove, reject) => {
        let item = that.data.images[0];
        let suffix = /\.\w+$/.exec(item)[0]; // 正则表达式，返回文件扩展名
        wx.cloud.uploadFile({
          cloudPath: that.data.userPhone+new Date().getTime() + suffix, // 上传至云端的路径
          filePath: item, // 小程序临时文件路径
          success: res => {
            // 返回文件 ID
            console.log(res.fileID)
            that.setData({
              head_img: that.data.head_img.concat(res.fileID)
            });
            reslove();
          },
          fail: console.error
        })
      }));
    }

    Promise.all(promiseArr).then(res => {

      wx.cloud.callFunction({
        name:'update',
        data:{
          phone:that.data.userPhone,
          pic:that.data.head_img[0],
        },success: res => {
          wx.cloud.deleteFile({
            fileList:tem_head_img
          }).then(res => {
            wx.hideLoading();
              wx.showToast({
                title: '更改成功',
              })
              app.userDate.userInfo.headPicture=that.data.head_img[0]
              app.formorPageLoad()
              wx.navigateBack({
                url: '../../userInfo/userInfo'
            })
            console.log(res.fileList)
          }).catch(error => {
            // handle error
          })
        },
        fail: err => {
          console.log(err)
        }
      })
    });
  }
  },

  previewImage: function (e) {
    app.previewImage(e)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    that.data.userPhone = options.num
    tem_head_img = tem_head_img.concat(options.pic)
    console.log("原来的头像",tem_head_img)
    that.setData({
      images:that.data.images.concat(options.pic)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})